package com.qixiucoding.context;

import com.qixiucoding.MInterface.BXCService;
import com.qixiucoding.util.MClassUtil;

import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class MyApplicationContext {
    private String PackageName;

    //bean容器
    private ConcurrentHashMap<String, Object> beanMap = null;

    public MyApplicationContext(String PackageName) throws Exception {
        if (PackageName == null || PackageName.equals("")) {
            throw new Exception("package name err");
        }
        this.PackageName = PackageName;
        initBean();
    }

    public Object getBean(String beanid) throws Exception {
        if (beanid==null||beanid.equals("")){
            throw new Exception("beanid not null");
        }
        Object o = beanMap.get(beanid);
        if (o==null){
            throw new Exception("class not find");
        }
        return o;
    }

    private void initBean() throws Exception {
        beanMap = getAllServiceClass();
        AttriAssign();
    }


    private void AttriAssign() throws IllegalAccessException {
        if (beanMap!=null&&beanMap.size()>0){
            for (Object o:beanMap.values()){
                // 获取类的属性是否存在 获取bean注解
                Class<? extends Object> classInfo = o.getClass();
                Field[] declaredFields = classInfo.getDeclaredFields();
                for (Field field : declaredFields) {
                    // 属性名称
                    String name = field.getName();
                    // 2.使用属性名称查找bean容器赋值
                    Object bean = beanMap.get(name);
                    if (bean != null) {
                        // 私有访问允许访问
                        field.setAccessible(true);
                        // 给属性赋值
                        field.set(o, bean);
                    }
                }
            }
        }
    }

    private ConcurrentHashMap<String,Object> getAllServiceClass() throws Exception {
        ConcurrentHashMap<String,Object> concurrentHashMap = null;
        List<Class<?>> allClasses = MClassUtil.getClasses(PackageName);
        if (allClasses==null||allClasses.size()==0){
            throw new Exception("package not class");
        }
        concurrentHashMap = new ConcurrentHashMap<String, Object>();
        for (Class<?> c : allClasses) {
            BXCService service = (BXCService)c.getDeclaredAnnotation(BXCService.class);
            if (service != null){
                Object newInstance = c.newInstance();
                //获取父类名称并转为首字母小写
                String beanId = toLowerCaseFirstOne(c.getSimpleName());
                concurrentHashMap.put(beanId, newInstance);
            }
        }
        return concurrentHashMap;
    }

    public static String toLowerCaseFirstOne(String s) {
        if (Character.isLowerCase(s.charAt(0))){
            return s;
        }
        else{
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }

}
